<?php

class Application_Model_Auth extends Zend_Db_Table_Abstract
{   
    //делаем выборку "Role"
    public function getRoleByIdUser($id) {
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $role = $this->db->query('SELECT roles.role 
                                  FROM roles, role_users
                                  WHERE roles.id = role_users.id_role AND 
                                  role_users.id_users =' . intval($id))->fetchAll();
        return $role;    
    }
    protected $_name = 'users';
  /* public function getRoleByIdUser($id) {
      $select = $this->select()
             ->from('roles')
             ->joinUsing('role_users', 'roles.id=roles_users.id_role')
             ->where('role_users.id_users = ?', $id )
             ->setIntegrityCheck(false); 
      return $this->fetchRow($select)->toArray();
     } */
    //считаем кол-во записей с ip пользователя
    public function checkError() {
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $ip = $_SERVER['REMOTE_ADDR'];
        $dataError = $this->db->query("SELECT ip 
                                       FROM authorization_error
                                       WHERE `ip` =" . floatval($ip));
        return $dataError->rowCount();
        
    }
    //добавление в базу ip, login, pass неудачно
	//авторизировавшегося польователя
	public function addError($login, $password) {
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $ip = $_SERVER['REMOTE_ADDR'];
        $data = array(
            'ip' => $ip,
            'login' => $login,
            'password' => $password,
            'time' => time());
        $this->db->insert('authorization_error',$data); 
    }
    
    //Удаление заблокированного ip адресса из базы данных
    public function deleteBlockedIp() {
        $ip = $_SERVER['REMOTE_ADDR'];
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $this->db->delete('authorization_error','ip =' . floatval($ip)); 
    }
    
    //Получаем время блокировки
    public function getTimeBlockedIp()	{
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $ip = $_SERVER['REMOTE_ADDR'];
        $dataError = $this->db->query("SELECT time 
                                       FROM authorization_error
                                       WHERE `ip` =" . floatval($ip));
        return $dataError->fetchAll();    
    }
    
    public function visitsCount($id, $current) {
        $data = array(
            'visits' => $current + 1
        );
        $this->update($data, 'id =' . intval($id));
    }   
}

